package club.cearnach.api.service.impl;


import club.cearnach.api.dao.IVisitorDao;
import club.cearnach.entity.Visitor;
import club.cearnach.api.form.PageBean;
import club.cearnach.api.service.IVisitorService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

/**
 * @author 阮胜
 * @date 2018/4/2 15:33
 */
@Service
public class VisitorServiceImpl implements IVisitorService {
    private final IVisitorDao visitorDao;

    public VisitorServiceImpl(IVisitorDao visitorDao) {
        this.visitorDao = visitorDao;
    }

    @Override
    public Visitor save(Visitor visitor) {
        return visitorDao.save(visitor);
    }

    @Override
    public long count() {
        return visitorDao.count();
    }

    @Override
    public long countByVisitDate() {
        return visitorDao.findTodayVisitorCount();
    }

    @Override
    public Page<Visitor> findAllOrderByVisitDate(PageBean pageBean) {
        return visitorDao.findAll(PageRequest.of(pageBean.getPage(), pageBean.getSize(), Sort.by(Sort.Direction.DESC,"visitDate")));
    }
}